package com.doublethread.easymart.modules.user.controller;

import com.doublethread.easymart.common.result.PageResult;
import com.doublethread.easymart.common.result.Result;
import com.doublethread.easymart.modules.user.dto.UserQueryDTO;
import com.doublethread.easymart.modules.user.dto.UserUpdateStatusDTO;
import com.doublethread.easymart.modules.user.service.UserService;
import com.doublethread.easymart.modules.user.vo.UserStatisticsVO;
import com.doublethread.easymart.modules.user.vo.UserVO;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 管理端用户控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/admin/users")
public class AdminUserController {

    @Autowired
    private UserService userService;

    /**
     * 获取用户列表
     */
    @GetMapping
    public Result<PageResult<UserVO>> getUserList(UserQueryDTO queryDTO) {
        log.info("查询用户列表");

        PageResult<UserVO> pageResult = userService.getUserList(queryDTO);

        log.info("查询用户列表成功");

        return Result.success(pageResult);
    }

    /**
     * 获取用户详情
     */
    @GetMapping("/{id}")
    public Result<UserVO> getUserDetail(@PathVariable Long id) {
        log.info("查询用户详情");

        UserVO userDetail = userService.getUserDetail(id);

        log.info("查询用户详情成功");

        return Result.success(userDetail);
    }

    /**
     * 更新用户状态
     */
    @PutMapping("/{id}/status")
    public Result<Void> updateUserStatus(@PathVariable Long id,
                                         @Valid @RequestBody UserUpdateStatusDTO statusDTO) {
        log.info("更新用户状态");

        userService.updateUserStatus(id, statusDTO);

        log.info("更新用户状态成功");

        return Result.success();
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/{id}")
    public Result<Void> deleteUser(@PathVariable Long id) {
        log.info("删除用户");

        userService.deleteUser(id);

        log.info("删除用户成功");

        return Result.success();
    }

    /**
     * 获取用户统计信息
     */
    @GetMapping("/statistics")
    public Result<UserStatisticsVO> getUserStatistics() {
        log.info("查询用户统计信息");

        UserStatisticsVO statistics = userService.getUserStatistics();

        log.info("查询用户统计信息成功");

        return Result.success(statistics);
    }
}
